//
//  SXPDbManager.swift
//  weibo
//
//  Created by shixinPeng on 16/3/13.
//  Copyright © 2016年 shixinPeng. All rights reserved.
//

import UIKit
import FMDB
/// 数据库管理类
class SXPDbManager: NSObject {

    //创建一个queue
    var queue: FMDatabaseQueue?
    
    //创建一个db
    var Db:FMDatabase?
    
    //创建一个管理的单例
    static let shareManager:SXPDbManager = SXPDbManager()
    //创建一个数据库
    func openDB(){
        //获取document路径
        let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last
        //拼接文件路径
        let dbPath = (path! as NSString).stringByAppendingPathComponent("status.db")
        
        //实例化,提供数据库路劲
        queue = FMDatabaseQueue(path: dbPath)
        
        //创建表格
        createTable()
    }
    
    private func createTable(){
        //使用sql语句
        let sql = "CREATE TABLE 't_Status' (" +
        "'status' text," +
        "'statusid' INTEGER NOT NULL," +
        "'userid' INTEGER," +
        " PRIMARY KEY('statusid'))"
        printLog("创建表的sql\(sql)")
        
        //运行sql
        queue?.inDatabase({ (Db) -> Void in
            Db.executeUpdate(sql, withArgumentsInArray: nil)
        })
        
    }
    
}
